/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-12
 * Time: 19:44
 */
public class Demo21 {
    public static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        //这种属于并发执行中间只有count++操作是是串行，i < 500000 和 i++ 都是并发的，执行速度更快
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                synchronized(locker) {
                    count++;
                }
            }
            System.out.println("t1线程结束");
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
            System.out.println("t2线程结束");
        });
        t1.start();
        t2.start();
        t1.join();;
        t2.join();
        System.out.println(count);
    }
}
